package titt.util.io.loader;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class HDDFileLoader implements FileStreamLoader {
	
	public FileStream loadFileStream(String filename) {
		File f = new File(filename);
		
		if(!f.exists())
			return null;

		return new FileStream(filename, this, f);
	}
	
	@Override
	public InputStream loadStream(String filename) {
		try {
			return new FileInputStream(new File(filename));
		}
		catch(Exception e) {
			return null;
		}
	}

	@Override
	public List<FileStream> loadDirectoryStream(String dirname) {
		File dir = new File(dirname);
		
		ArrayList<FileStream> result = new ArrayList<FileStream>();
		
		if(!(dir.exists() && dir.isDirectory()))
			return result;
		
		for(File f : dir.listFiles())
			if(f.isDirectory())
				result.addAll(loadDirectoryStream(f.getAbsolutePath()));
			else
				result.add(loadFileStream(f.getAbsolutePath()));
		
		return result;
	}
	
}
